package se.cambio.system.rms.bo.impl;

import org.springframework.transaction.annotation.Transactional;

import se.cambio.system.rms.bo.HospitalBo;
import se.cambio.system.rms.dao.HospitalDao;
import se.cambio.system.rms.entity.Hospital;


@Transactional
public class HospitalBoImpl implements HospitalBo{
	
	HospitalDao hospitalDao;
	
	public void setHospitalDao(HospitalDao hospitalkDao) {
		this.hospitalDao = hospitalkDao;
	}

	public void save(Hospital hospital){
		Hospital hospital_new = hospital;
		Hospital hospital_old_exist;
		try{
			hospital_old_exist= hospitalDao.findByHospitalId(hospital_new.getId());
			
		}catch(NullPointerException e){
			hospitalDao.save(hospital);
		}
		System.out.println("already exist!");
	}
	
	public void update(Hospital hospital){
		Hospital hospital_new = hospital;
		
		Hospital hospital_old_latest_active = hospitalDao.findByHospitalId(hospital_new.getId());
		hospital_old_latest_active.setLatest(0);
		hospitalDao.update(hospital_old_latest_active);
		
		hospitalDao.save(hospital_new);
	}
	
	public void delete(Hospital hospital){
		Hospital hospital_new = hospital;
		
		Hospital hospital_old_latest_active = hospitalDao.findByHospitalId(hospital_new.getId());
		hospital_old_latest_active.setLatest(0);
		hospitalDao.update(hospital_old_latest_active);
		
		hospital_new.setActive(0);
		hospitalDao.save(hospital_new);
	}
	
	public Hospital findByHospitalId(String id){
		return hospitalDao.findByHospitalId(id);
	}
}
